package com.example.wotu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class LoginScr extends AppCompatActivity {
    private final int tim=3000;
    private static int ii=3,tf;
    private SQLiteDatabase sql;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_scr);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //更换图片
        SimpleDateFormat sl=new SimpleDateFormat("HH");     //根据手机上的时间
        int s=Integer.parseInt(sl.format(new Date()));
        ImageView i2=(ImageView)findViewById(R.id.imageView2);
        if(s<6){
            i2.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),R.mipmap.log));
        }else if(s<12){
            i2.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),R.mipmap.log2));
        }else if(s<18){
            i2.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),R.mipmap.log3));
        }else if(s<24){
            i2.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),R.mipmap.log1));
        }

        //倒计时（开启线程计算）
        ii=3;
        if(ii==3){
            final Handler handler=new Handler();
            final Runnable runnable=new Runnable() {
                @Override
                public void run() {
                    handler.postDelayed(this,1000);
                    ProgressBar logtime=(ProgressBar) findViewById(R.id.logtime);
                    if(ii==0){
                        handler.removeCallbacks(this);
                        if(tf>0){
                            Intent in=new Intent(LoginScr.this,MainActivity.class);
                            Bundle bu=new Bundle();
                            bu.putInt("useid",tf);
                            in.putExtras(bu);
                            startActivity(in);
                        }
                        else{
                            startActivity(new Intent(LoginScr.this,LogoinScreen.class));
                        }
                    }else if(ii==2){
                        tf=selectSQL();
                    }
                    logtime.incrementProgressBy(33);
                    ii--;
                }
            };
            handler.postDelayed(runnable,1000);     //必须要一次外调用
        }else{
            onStart();
        }
    }

    public int selectSQL(){
        sql = new MySQLOpen(LoginScr.this).getReadableDatabase();
        try{
            Cursor c = sql.rawQuery("select useid,finaltime,usetruef from use where usetruef = 1",null);
            if(c.moveToFirst()){
//                String[] time=c.getString(c.getColumnIndex("finaltime")).split("-");
                String[] time=c.getString(1).split("-");
                Calendar cal=Calendar.getInstance();
                Calendar cal2=Calendar.getInstance();
                cal2.set(Integer.parseInt(time[0]),Integer.parseInt(time[1]),Integer.parseInt(time[2])+2);   //加后日期
                int in=(int)(cal2.getTimeInMillis()-cal.getTimeInMillis())/(24*60*60*1000);
                if(in<3) tf=c.getInt(0);
                else tf=0;
            }else tf = 0;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sql.close();
        }
        return tf;
    }
}